package com.zp.business.impl.register;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.business.entity.register.ClinicMasterCost;
import com.zp.business.entity.register.bo.ClinicMasterCostBo;
import com.zp.business.entity.register.vo.ClinicMasterCostVo;
import com.zp.business.mapper.register.ClinicMasterCostMapper;
import com.zp.business.service.register.IClinicMasterCostService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 门诊挂号费用明细Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class ClinicMasterCostServiceImpl implements IClinicMasterCostService {

    private final ClinicMasterCostMapper baseMapper;

    /**
     * 查询门诊挂号费用明细
     */
    @Override
    public ClinicMasterCostVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询门诊挂号费用明细列表(分页)
     */
    @Override
    public TableDataInfo<ClinicMasterCostVo> queryPageList (ClinicMasterCostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ClinicMasterCost> lqw = buildQueryWrapper(bo);
        Page<ClinicMasterCostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门诊挂号费用明细列表(不分页)
     */
    @Override
    public List<ClinicMasterCostVo> queryList (ClinicMasterCostBo bo) {
        LambdaQueryWrapper<ClinicMasterCost> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ClinicMasterCost> buildQueryWrapper (ClinicMasterCostBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ClinicMasterCost> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPatientId()), ClinicMasterCost::getPatientId, bo.getPatientId());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicTempId()), ClinicMasterCost::getClinicTempId, bo.getClinicTempId());
        lqw.eq(StringUtils.isNotBlank(bo.getItemClass()), ClinicMasterCost::getItemClass, bo.getItemClass());
        lqw.like(StringUtils.isNotBlank(bo.getPackItemName()), ClinicMasterCost::getPackItemName, bo.getPackItemName());
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), ClinicMasterCost::getItemName, bo.getItemName());
        lqw.eq(StringUtils.isNotBlank(bo.getItemCode()), ClinicMasterCost::getItemCode, bo.getItemCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPriceId()), ClinicMasterCost::getPriceId, bo.getPriceId());
        lqw.eq(bo.getNetworkPrice() != null, ClinicMasterCost::getNetworkPrice, bo.getNetworkPrice());
        lqw.eq(bo.getTradePrice() != null, ClinicMasterCost::getTradePrice, bo.getTradePrice());
        lqw.eq(bo.getPrice() != null, ClinicMasterCost::getPrice, bo.getPrice());
        lqw.eq(bo.getCosts() != null, ClinicMasterCost::getCosts, bo.getCosts());
        lqw.eq(bo.getCharges() != null, ClinicMasterCost::getCharges, bo.getCharges());
        lqw.eq(bo.getDiffCost() != null, ClinicMasterCost::getDiffCost, bo.getDiffCost());
        lqw.eq(bo.getNum() != null, ClinicMasterCost::getNum, bo.getNum());
        lqw.eq(bo.getMedicalScale() != null, ClinicMasterCost::getMedicalScale, bo.getMedicalScale());
        lqw.eq(StringUtils.isNotBlank(bo.getRcptNo()), ClinicMasterCost::getRcptNo, bo.getRcptNo());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnRcpt()), ClinicMasterCost::getClassOnRcpt, bo.getClassOnRcpt());
        lqw.eq(StringUtils.isNotBlank(bo.getClassOnReckoning()), ClinicMasterCost::getClassOnReckoning, bo.getClassOnReckoning());
        lqw.eq(StringUtils.isNotBlank(bo.getSubjCode()), ClinicMasterCost::getSubjCode, bo.getSubjCode());
        lqw.eq(bo.getCoefficient() != null, ClinicMasterCost::getCoefficient, bo.getCoefficient());
        lqw.eq(StringUtils.isNotBlank(bo.getSpec()), ClinicMasterCost::getSpec, bo.getSpec());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), ClinicMasterCost::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), ClinicMasterCost::getOrgId, bo.getOrgId());
        lqw.eq(bo.getAbidance() != null, ClinicMasterCost::getAbidance, bo.getAbidance());
        lqw.like(StringUtils.isNotBlank(bo.getChannel()), ClinicMasterCost::getChannel, bo.getChannel());
        lqw.like(StringUtils.isNotBlank(bo.getPerformFreq()), ClinicMasterCost::getPerformFreq, bo.getPerformFreq());
        lqw.eq(bo.getSingleDosage() != null, ClinicMasterCost::getSingleDosage, bo.getSingleDosage());
        lqw.like(StringUtils.isNotBlank(bo.getSingleUnitName()), ClinicMasterCost::getSingleUnitName, bo.getSingleUnitName());
        lqw.eq(StringUtils.isNotBlank(bo.getImplementUser()), ClinicMasterCost::getImplementUser, bo.getImplementUser());
        lqw.eq(StringUtils.isNotBlank(bo.getImplementDept()), ClinicMasterCost::getImplementDept, bo.getImplementDept());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), ClinicMasterCost::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getIsUpload()), ClinicMasterCost::getIsUpload, bo.getIsUpload());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicalVerifyFlag()), ClinicMasterCost::getMedicalVerifyFlag, bo.getMedicalVerifyFlag());
        return lqw;
    }

    /**
     * 新增门诊挂号费用明细
     */
    @Override
    public Boolean insertByBo (ClinicMasterCostBo bo) {
        ClinicMasterCost add = BeanUtil.toBean(bo, ClinicMasterCost.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改门诊挂号费用明细
     */
    @Override
    public Boolean updateByBo (ClinicMasterCostBo bo) {
        ClinicMasterCost update = BeanUtil.toBean(bo, ClinicMasterCost.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (ClinicMasterCost entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门诊挂号费用明细
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
